package com.ms.service.module.broadcastlistener;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.Jedis;

import com.ms.service.db.jedis.JedisManager;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

/**
 * 將登入或登出廣播收聽的module從redis新增或移除
 */
public class LoginLogoutRedisAsync extends TaskAsyncCommand{

	private static final Log log = LogFactory.getLog(LoginLogoutRedisAsync.class);
	
	private String redisKeys;
	
	private String moduleName;
	
	private int type = 0;
	
	@Override
	public void asyncExecute() throws Exception {
		if(redisKeys == null){
			if(log.isErrorEnabled()){
				log.error("redisKeys is null");
			}
			return;
		}
		
		//以分割號處理redisKeys，看看這次async要處理幾個key
		String[] keys = redisKeys.split(LoginLogoutListener.MULTI_KEY_SPLITER);
		
		switch (type) {
			case LoginLogoutListener.ADD:{
				addInRedis(keys, moduleName);
				break;
			}
			case LoginLogoutListener.REMOVE:{
				removeInRedis(keys, moduleName);
				break;
			}
			case LoginLogoutListener.DELETE_KEY:{
				removeRedisKey(keys);
				break;
			}
				
			default:{
				if(log.isErrorEnabled()){
					log.error("can't recognize LoginLogoutListener type, type:" + type);
				}
				return;
			}
			
		}
		
	}

	@Override
	public void execute() throws Exception {
		
	}

	/**
	 * 新增moduleName到該key的list內
	 */
	private static void addInRedis(String[] keys, String moduleName){
		if(moduleName == null)
			return;
		
		//使用login的redis連線資訊與對應的dbid
		Jedis jedis = JedisManager.getResource(ModuleName.LOGIN);
		
		//對每一個key
		for(String key : keys){
			//先讀出來判斷
			List<String> moduleNames = (List<String>) jedis.lrange(key, 0, -1);
			if(!moduleNames.contains(moduleName)){
				//寫入新的
				jedis.rpush(key, moduleName);
				
				if(log.isInfoEnabled()){
					log.info("add ["+moduleName+"] to key:"+key);
				}
			}
			else{
				if(log.isInfoEnabled()){
					log.info("there is another ["+moduleName+"] in this key:"+key);
				}
			}
		}
		
		JedisManager.returnResource(ModuleName.LOGIN, jedis);
	}
	
	/**
	 * 刪除moduleName存在於該key的list
	 */
	private static void removeInRedis(String[] keys, String moduleName){
		if(moduleName == null)
			return;
		
		//使用login的redis連線資訊與對應的dbid
		Jedis jedis = JedisManager.getResource(ModuleName.LOGIN);
		
		for(String key : keys){
			//removeCount = lrem指令移除的元素數量
			long removeCount = jedis.lrem(key, 0, moduleName);
			
			if(removeCount != 1){
				if(log.isWarnEnabled()){
					log.warn("remove ["+moduleName+"] element count is " + removeCount + ", key:" + key);
				}
			}
			else {
				if(log.isInfoEnabled()){
					log.info("remove ["+moduleName+"] from key:"+key);
				}
			}
		}

		JedisManager.returnResource(ModuleName.LOGIN, jedis);
	}
	
	/**
	 * 刪除在REDIS的key
	 */
	private static void removeRedisKey(String[] keys){
		//使用login的redis連線資訊與對應的dbid
		Jedis jedis = JedisManager.getResource(ModuleName.LOGIN);
		
		for(String key : keys){
			if(jedis.exists(key)){
				long removeCount = jedis.del(key);
				if(removeCount != 1){
					if(log.isWarnEnabled()){
						log.warn("remove key count is " + removeCount+", key:"+key);
					}
				}
			}
		}

		JedisManager.returnResource(ModuleName.LOGIN, jedis);
	}
	
	public void setRedisKeys(String redisKeys) {
		this.redisKeys = redisKeys;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public void setType(int type) {
		this.type = type;
	}

}
